package Search;

import Data.ChatData;
import Data.PrivateInformationData;
import Server.ManageClientConnectionServerThread;

import java.io.*;
import java.nio.file.Files;
import java.sql.*;

public class SearchFriend {
    private String Number;
    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";
    ChatData chatData;

    public SearchFriend(ChatData chatData) {
        this.chatData=chatData;
    }

    public String getNumber() {
        return Number;
    }

    public void setNumber(String number) {
        Number = number;
    }

    public PrivateInformationData returnFriend() throws ClassNotFoundException, SQLException, IOException {
        PrivateInformationData frienddata=new PrivateInformationData();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);

        PreparedStatement operation=null;
        String sql = "select *FROM userinformation where id=? or emailNumber=?;";
        operation = Conn.prepareStatement(sql);
        operation.setString(1,chatData.getReceiver_id());
        operation.setString(2,chatData.getReceiver_id());
        ResultSet resultSet=operation.executeQuery();
        System.out.println(Number);
        if(resultSet.next())
        {
            //这个仅仅是代表有这个人
            System.out.println("找到了");
            frienddata.setSex(resultSet.getString("sex"));
            frienddata.setSaying(resultSet.getString("saying"));

            String path=resultSet.getString("avatar");
            File file=new File(path);

            byte[] t=null;
            if(file!=null)
            {
                byte[] buffer=new byte[1024];
                t=new byte[(int)file.length()];
                InputStream is= Files.newInputStream(file.toPath());
                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                int len;
                while((len=is.read(buffer))!=-1)
                {
                    bos.write(buffer,0,len);
                }
                t=bos.toByteArray();
            }
            frienddata.setAvatar(t);

            frienddata.setUserid(resultSet.getString("id"));
            frienddata.setUserNickName(resultSet.getString("userNickName"));
            frienddata.setEmailNumber(resultSet.getString("emailNumber"));
            System.out.println(frienddata.getUserid());

            String presql="SELECT *FROM friendlist where (userId=? and userFId=? ) or (userId=? and userFId=? );";

            //在判断一下是否已经是好友关系
            operation=Conn.prepareStatement(presql);
            operation.setString(1,chatData.getSender_id());
            operation.setString(2,frienddata.getUserid());
            operation.setString(3,frienddata.getUserid());
            operation.setString(4,chatData.getSender_id());
            resultSet=operation.executeQuery();
            if(resultSet.next())
            {
                //他们已经是好友关系
                frienddata.setUserid("");
            }


        }
        else
        {
            frienddata.setUserid("");
        }
        Conn.close();
        operation.close();
        return frienddata;
    }
}
